package br.senai.sc.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;

/**
 * @author Fernando Marquardt &lt;fernando.marquardt@sc.senai.br&gt;
 * @version $Id: Usuario.java 28 2012-03-07 20:48:20Z fernando.marquardt $
 */
@Entity
@Table(name = "usuario")
@NamedQueries({
    @NamedQuery(name = Usuario.USUARIOS, query = "SELECT u FROM Usuario u ORDER BY u.idUsuario"),
    @NamedQuery(name = Usuario.USUARIO_BY_LOGIN, query = "SELECT u FROM Usuario u WHERE u.login = :login")
})
public class Usuario implements Serializable {

    private static final long serialVersionUID = 1L;

    public static final String USUARIOS = "USUARIOS";
    public static final String USUARIO_BY_LOGIN = "USUARIO_BY_LOGIN";

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id_usuario")
    private Integer idUsuario;

    @NotNull
    @ManyToOne
    @JoinColumn(name = "id_permissao")
    private Permissao permissao;

    @NotBlank
    @Length(max = 150)
    private String nome;

    @NotBlank
    @Length(min = 3, max = 50)
    private String login;

    @NotBlank
    @Length(max = 50)
    private String senha;

    public Usuario() {}

    public Integer getIdUsuario() {
        return idUsuario;
    }

    public void setIdUsuario(Integer idUsuario) {
        this.idUsuario = idUsuario;
    }

    public Permissao getPermissao() {
        return permissao;
    }

    public void setPermissao(Permissao permissao) {
        this.permissao = permissao;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getSenha() {
        return senha;
    }

    public void setSenha(String senha) {
        this.senha = senha;
    }

    @Override
    public String toString() {
        return "Usuario [idUsuario=" + idUsuario + ", nome=" + nome + ", login=" + login + "]";
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((login == null) ? 0 : login.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        Usuario other = (Usuario) obj;
        if (login == null) {
            if (other.login != null) {
                return false;
            }
        } else if (!login.equals(other.login)) {
            return false;
        }
        return true;
    }
}